<?php

class Zend_View_Helper_JQueryUiAjaxTabs extends ZendX_JQuery_View_Helper_UiWidget
{
	
	protected $_namePane;
	protected $_nameContainer;
	
	public function jqueryUiAjaxTabs($name,Array $options)
	{
		$this->_namePane = $name.'UITabPane';
		$this->_nameContainer = $name.'UITabContainer';
		
		$this->_initJavaScript();
		$this->_initHtml($options);
		
	}
	
	private function _initJavaScript() {
		$script = "
			//JQuery UI Tabs
		 	$(document).ready(function(){
		 		var div = '#".$this->_nameContainer."';
				$('#".$this->_namePane."').tabs({
				    load: function(event, ui) {
				    	if(this.value == 'undefined') {
					        $('a', ui.panel).click(function() {
								$.ajax( {
									url : this.href,
									beforeSend : function() {},
									complete : function() {},
									success : function(conteudo) {
										$(div).html(conteudo);
									}
								})						
					            return false;
					        });
				    	}
				    }
				});
        	});
		";
		$this->jquery->addOnLoad($script);
	}
	
	private function _initHtml(Array $options) {
		$baseUrl = Zend_Controller_Front::getInstance()->getBaseUrl();
		
		echo '<div id="'.$this->_namePane.'">'.PHP_EOL;
			echo '<ul>'.PHP_EOL;
				foreach($options as $option) {
					$url = $this->view->url(array('module' => $option['module'], 'controller'=> $option['controller'],'action'=> $option['action'],'id'=> $option['id']), null, true);
					echo '<li><a href="'.$url.'" >'.$option['label'].'
					</a></li>';
				}
		
			echo '</ul>'.PHP_EOL;
		echo '</div>'.PHP_EOL;
		
		echo '<div id="'.$this->_nameContainer.'"></div>'.PHP_EOL;
	}
	
}